﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Belmost4PDA.Fasade;

namespace Belmost4PDA.Logic
{
    /// <summary>
    /// Просто чек бокс (не выключаеться если в его группе ктото включился)
    /// </summary>
    public class defStrategyCheck:defControlStrategy
    {
        protected readonly HintCheckBox checkbox = null;
        public HintView Hint=null;
        public defStrategyCheck(DefectsReader.brDefItem brDefItem, DefectsReader.DefItem defItem, brDef brDefOwner):base(brDefOwner)
        {
            Hint=new HintView();
            checkbox = new HintCheckBox();
            HintMenu cont = new HintMenu(checkbox, Hint);
            checkbox.ContextMenu = cont;
            checkbox.Text = defItem.defNPP + " " + defItem.defNm;
            _isChecked = brDefItem.defOn;
            checkbox.Checked = brDefItem.defOn;
            checkbox.CheckStateChanged += checkbox_CheckStateChanged;
            
        }

        public override void AddToForm(Form _form)
        {
            int RelY = (int) _form.Tag;
            RelY += 5;
            checkbox.Left = 5;
            checkbox.Width = _form.Width - 10;
            checkbox.Top = RelY;
            RelY += checkbox.Height;
            //Hint
            Hint.BackColor = Color.LightGoldenrodYellow;
            Hint.Width = _form.Width - 10;
            Hint.Left = 5;
            Hint.Height = 1;
            Hint.Top = RelY;
            Hint.Visible = false;
            Hint.Click += new EventHandler(Hint_Click);
            _form.Controls.Add(checkbox);
            _form.Controls.Add(Hint);
            
            _form.Tag = RelY;
        }
        void Hint_Click(object sender, EventArgs e)
        {
            Hint.Visible = false;
        }

        public override void FromControls()
        {
            isChecked = checkbox.Checked;
        }

        public override void OnChange(defControlStrategy invoker)
        {
            if (invoker == this)
                foreach (brDef def in invoker.defItem.parentElement.defList)
                {
                    if (def != this.defItem)
                    {
                        if ((def.DefItem.isRadio) || (defItem.DefItem.isRadio))
                            if ((GroupID == def.DefItem.rootDef))
                            {
                                if(!this.checkbox.Checked)
                                def.CheckOff();
                            }
                    }
                }
        }
        public override void CheckOff()
        {
            checkbox.Checked = false;
        }
    }
}
